package com.zg.api.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.zg.api.feign.MicroFeignClient;
import com.zg.api.service.ApiProjectService;
import com.zg.api.sys.ApiE;
import com.zg.common.core.dao.IDao;
import com.zg.common.core.dao.apis.ApiProjectDao;
import com.zg.common.core.dao.apis.entity.ApiProject;
import com.zg.common.core.service.PageBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/6/16 20:11
 */

@Slf4j
@Service
public class ApiProjectServiceImpl extends PageBaseService<ApiProject, ApiProjectDao> implements ApiProjectService {


//    @Autowired
//    @Qualifier("apiProjectDao")
//    @Override
//    protected void setDao(IDao<ApiProject> dao) {
//        this.dao = dao;
//    }

//    @Autowired
//    private MicroFeignClient microFeignClient;

//    @Override
//    public  LambdaQueryWrapper<ApiProject> buildQueryWrapper(ApiProject project){
//        LambdaQueryWrapper<ApiProject> qw = new LambdaQueryWrapper<>();
//        if(project!=null){
//            qw.like(!StrUtil.isEmpty(project.getProjectName()), ApiProject::getProjectName, project.getProjectName());
//            qw.eq(!StrUtil.isEmpty(project.getProjectCode()), ApiProject::getProjectCode, project.getProjectCode());
//        }
//
//        return qw;
//    }

    public List<ApiProject> listByName(String name){
        LambdaQueryWrapper<ApiProject> qw = new LambdaQueryWrapper<>();
        qw.eq(ApiProject::getProjectName,name);
        return baseDao.list(qw);
    }

    public ApiProject getByCode(String code){
        LambdaQueryWrapper<ApiProject> qw = new LambdaQueryWrapper<>();
        qw.eq(ApiProject::getProjectCode,code);
        return baseDao.getOne(qw);
    }


    @Override
    protected void preAdd(ApiProject source) {
        ApiProject project = getByCode(source.getProjectCode());
        if(project!=null){
            ApiE.PROJECT_IS_EXIST.happen();
        }
    }

    public void changeNamespace(List<Long> ids,boolean flag){
        List<ApiProject> apiProjectList= baseDao.listByIds(ids);
        apiProjectList.stream().forEach(t->{
            t.setEnNamespace(flag);
            baseDao.updateById(t);
        });
    }
    @Override
    protected void afterAdd(ApiProject source) {
//        NamespaceAddReq nameSpaceEditReq = new NamespaceAddReq();
//        nameSpaceEditReq.setCustomNamespaceId(source.getProjectCode());
//        nameSpaceEditReq.setNamespaceName(source.getProjectName());
//        nameSpaceEditReq.setNamespaceDesc(source.getIntro());
//        microFeignClient.addNamespaces(nameSpaceEditReq);
    }

    @Override
    protected void afterEdit(ApiProject source) {
//        NamespaceEditReq nameSpaceEditReq = new NamespaceEditReq();
//        nameSpaceEditReq.setNamespace(source.getProjectCode());
//        nameSpaceEditReq.setNamespaceShowName(source.getProjectName());
//        nameSpaceEditReq.setNamespaceDesc(source.getIntro());
//        microFeignClient.editNamespaces(nameSpaceEditReq);
    }

    @Override
    protected void preRemove(List<Long> ids) {
        List<ApiProject> list = baseDao.listByIds(ids);
        for(int i=0;i<list.size();i++){
            ApiProject apiProject = list.get(i);
            if(apiProject.isEnNamespace()){
                ApiE.PROJECT_IS_NAMESPACE.happen();
            }
        }
    }

}
